package com.luoyuanxiangvip.web.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.luoyuanxiangvip.common.enums.ConfigEnum;
import com.luoyuanxiangvip.common.utils.AjaxResult;
import com.luoyuanxiangvip.common.vo.BizCommentVO;
import com.luoyuanxiangvip.common.vo.OtherOption;
import com.luoyuanxiangvip.common.vo.SiteSettings;
import com.luoyuanxiangvip.core.entity.BizComment;
import com.luoyuanxiangvip.core.service.IBizCommentService;
import com.luoyuanxiangvip.core.service.IConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author luoyuanxiang <p>luoyuanxiangvip.com</p>
 * @since 2019-05-26
 */
@RestController
@Api(description = "评论管理")
public class BizCommentController{

    @Resource
    @Lazy
    private IBizCommentService iBizCommentService;
    @Resource
    @Lazy
    private IConfigService iConfigService;

    @PostMapping("/comment/list")
    @ApiOperation(value = "获取评论列表")
    public AjaxResult comment(@RequestBody BizCommentVO bizCommentVO) {
        IPage<BizComment> page = iBizCommentService.findPageBreakByCondition(bizCommentVO);
        return AjaxResult.success("获取评论列表成功", page);
    }

    @PostMapping("/comment/save")
    @ApiOperation(value = "添加评论")
    public AjaxResult commentSave(@RequestBody BizComment comment) {
        OtherOption otherOption = iConfigService.getValue(ConfigEnum.OTHER_OPTION.getKey());
        BizComment bizComment = iBizCommentService.commentSave(otherOption.getAmapApiAk(), comment);
        return AjaxResult.success("添加评论成功", bizComment);
    }

    @PatchMapping("/comment/likes/{id}")
    @ApiOperation(value = "喜欢评论")
    public AjaxResult commentLikes(@PathVariable("id") Long id) {
        BizComment byId = iBizCommentService.getById(id);
        byId.setSupport(byId.getSupport() + 1);
        iBizCommentService.updateById(byId);
        return AjaxResult.success();
    }

    @PatchMapping("/like/site")
    @ApiOperation(value = "留言板喜欢人数")
    public AjaxResult guestbookLikes() {
        SiteSettings siteSettings = iConfigService.getValue(ConfigEnum.SITE_SETTINGS.getKey());
        siteSettings.setLikes(siteSettings.getLikes() + 1);
        boolean update = iConfigService.update().set("rel_value", JSONObject.toJSONString(siteSettings)).eq("rel_key", ConfigEnum.SITE_SETTINGS.getKey()).update();
        return update ? AjaxResult.success("修改系统配置成功") : AjaxResult.error("修改系统配置失败");
    }

}
